package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.tongdun.TongDunRiskRecord;
import com.ucredit.babel.common.model.third.tongdun.details.TongDunDetailsCondition;
import com.ucredit.babel.common.model.third.tongdun.details.TongDunDetailsResult;
import com.ucredit.babel.common.model.third.tongdun.details.TongDunDetailsRule;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.List;

/**
 * 同盾-命中规则详情查询
 *
 * @author guoqiang
 */

public class TongDunRiskConditionsV2Parse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();
    private final int feature = SerializerFeature.WRITE_MAP_NULL_FEATURES;

    /**
     * 同盾-命中规则详情查询
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        TongDunRiskRecord obj = (TongDunRiskRecord) model;
        TongDunDetailsResult risk_obj = obj.getHit_rule_result() == null ? new TongDunDetailsResult() : obj.getHit_rule_result();
        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder txSb = getBaseInfo(obj);

        StringBuilder TxSb = new StringBuilder(500);
        List<TongDunDetailsRule>  rules = risk_obj.getRules();
        if(rules == null){
            return true;
        }
        setFileNum(10);
        for(TongDunDetailsRule rule: rules) {
            List<TongDunDetailsCondition> conditions = rule.getConditions();
            if(!conditions.isEmpty()){
                for(TongDunDetailsCondition condition: conditions) {
                    TxSb.delete(0, TxSb.length());
                    TxSb.append(formatValue(risk_obj.isSuccess())).append(CRT001);
                    TxSb.append(formatValue(risk_obj.getReason_code())).append(CRT001);
                    TxSb.append(formatValue(risk_obj.getReason_desc())).append(CRT001);
                    TxSb.append(formatValue(rule.getRule_id())).append(CRT001);
                    TxSb.append(formatValue(rule.getDecision())).append(CRT001);
                    TxSb.append(formatValue(rule.getScore())).append(CRT001);
                    TxSb.append(formatValue(condition.getType())).append(CRT001);
                    TxSb.append(formatValue(condition.getAbnormal_tags())).append(CRT001);
                    TxSb.append(formatValue(condition.getAddress_a())).append(CRT001);
                    TxSb.append(formatValue(condition.getAddress_a_value())).append(CRT001);
                    TxSb.append(formatValue(condition.getAddress_b())).append(CRT001);
                    TxSb.append(formatValue(condition.getAddress_b_value())).append(CRT001);
                    TxSb.append(formatValue(condition.getBlack_cnt())).append(CRT001);
                    TxSb.append(formatValue(condition.getBlack_cnt_one())).append(CRT001);
                    TxSb.append(formatValue(condition.getBlack_cnt_two())).append(CRT001);
                    TxSb.append(formatValue(condition.getBlack_dst())).append(CRT001);
                    TxSb.append(formatValue(condition.getBlack_rat())).append(CRT001);
                    TxSb.append(formatValue(condition.getCalc_dim_type())).append(CRT001);
                    TxSb.append(formatValue(condition.getCalc_dim_type_value())).append(CRT001);
                    TxSb.append(formatValue(condition.getCalc_type())).append(CRT001);
                    TxSb.append(formatValue(condition.getCode())).append(CRT001);
                    TxSb.append(formatValue(condition.getCode_display_name())).append(CRT001);
                    TxSb.append(formatValue(condition.getCore_dst())).append(CRT001);
                    TxSb.append(formatValue(condition.getCore_node())).append(CRT001);
                    TxSb.append(formatValue(condition.getData())).append(CRT001);
                    TxSb.append(formatValue(condition.getDegree())).append(CRT001);
                    TxSb.append(formatValue(condition.getDesc())).append(CRT001);
                    TxSb.append(formatValue(condition.getDiff_display())).append(CRT001);
                    TxSb.append(formatValue(condition.getDim_type())).append(CRT001);
                    TxSb.append(formatValue(condition.getDim_value())).append(CRT001);
                    TxSb.append(formatValue(condition.getEmulator_type())).append(CRT001);
                    TxSb.append(formatValue(condition.getEvent_type())).append(CRT001);
                    TxSb.append(formatValue(condition.getFraud_dist())).append(CRT001);
                    TxSb.append(formatValue(condition.getFraud_dist_one())).append(CRT001);
                    TxSb.append(formatValue(condition.getFraud_dist_two())).append(CRT001);
                    TxSb.append(formatValue(condition.getGps_a())).append(CRT001);
                    TxSb.append(formatValue(condition.getGps_b())).append(CRT001);
                    TxSb.append(formatValue(condition.getGrey_cnt())).append(CRT001);
                    TxSb.append(formatValue(condition.getGrey_rat())).append(CRT001);
                    TxSb.append(formatValue(condition.getGroup_id())).append(CRT001);
                    TxSb.append(condition.getHits() == null ? "" : JSON.toJSONString(condition.getHits(), feature)).append(CRT001);
                    TxSb.append(condition.getHits_for_dim() == null ? "" : JSON.toJSONString(condition.getHits_for_dim(), feature)).append(CRT001);
                    TxSb.append(formatValue(condition.getHook_address())).append(CRT001);
                    TxSb.append(formatValue(condition.getHook_i_m_p())).append(CRT001);
                    TxSb.append(formatValue(condition.getHook_inline())).append(CRT001);
                    TxSb.append(formatValue(condition.getHook_method())).append(CRT001);
                    TxSb.append(condition.getList() == null ? "" : JSON.toJSONString(condition.getList(), feature)).append(CRT001);
                    TxSb.append(formatValue(condition.getMatch_dim_type())).append(CRT001);
                    TxSb.append(formatValue(condition.getMatch_sub_dim_type())).append(CRT001);
                    TxSb.append(formatValue(condition.getNode_dist())).append(CRT001);
                    TxSb.append(formatValue(condition.getNode_score())).append(CRT001);
                    TxSb.append(formatValue(condition.getProxy_ip_type())).append(CRT001);
                    TxSb.append(formatValue(condition.getReplace_text())).append(CRT001);
                    TxSb.append(formatValue(condition.getResult())).append(CRT001);
                    TxSb.append(condition.getResults_for_dim() == null ? "" : JSON.toJSONString(condition.getResults_for_dim(), feature)).append(CRT001);
                    TxSb.append(formatValue(condition.getSub_dim_type())).append(CRT001);
                    TxSb.append(formatValue(condition.getTotal_cnt())).append(CRT001);
                    TxSb.append(formatValue(condition.getTotal_cnt_two())).append(CRT001);
                    TxSb.append(formatValue(condition.getUnit())).append(CRT001);
                    TxSb.append(formatValue(condition.getUnit_display_name())).append(CRT001);
                    TxSb.append(formatValue(condition.getValue())).append(CRT001);
                    key.set(getKey());
                    val.set((sb.toString() + TxSb + txSb).replace("\n", " ").replace("\r", " "));
                    context.write(key, val);
                }
            } else{
                TxSb.delete(0, TxSb.length());
                TxSb.append(formatValue(risk_obj.isSuccess())).append(CRT001);
                TxSb.append(formatValue(risk_obj.getReason_code())).append(CRT001);
                TxSb.append(formatValue(risk_obj.getReason_desc())).append(CRT001);
                TxSb.append(formatValue(rule.getRule_id())).append(CRT001);
                TxSb.append(formatValue(rule.getDecision())).append(CRT001);
                TxSb.append(formatValue(rule.getScore())).append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                key.set(getKey());
                val.set((sb.toString() + TxSb + txSb).replace("\n", " ").replace("\r", " "));
                context.write(key, val);
            }
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tongdun_risk_conditions_v2_inc";
    }

    @Override
    public String getSubType() {
        return ".conditions";
    }

    @Override
    public String getType() {
        return RecordType.TONGDUN_RISK_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "同盾-命中规则详情查询";
    }
}
